<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

const animationDuration = 6000;

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      this.chart.setOption({
        title: {
          text: this.$t("dashboard.workYears"),
        },
        tooltip: {
          trigger: "axis",
          formatter: "{a} <br/>{b} : {c}",
        },
        grid: {
          top: 70,
          containLabel: true,
          bottom: 50,
        },
        xAxis: [
          {
            type: "category",
            data: ["< 1", "1-3", "3-5", "5-10"],
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            axisTick: {
              show: false,
            },
          },
        ],
        series: [
          {
            type: "bar",
            stack: "vistors",
            barWidth: "60%",
            data: [79, 52, 100, 34],
            animationDuration,
          },
        ],
      });
    },
  },
};
</script>
